<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * UserController
 * 用户注册登录及用户后台操作
 * @author hansir
 * 
 * 
 **/

class UserController extends Controller {
	/**
	 * 测试
	 * 
	 */
    public function test(){
    	$this->display();
    }
	/**
	 * 展示我的游记
	 * @param str       $name  获取cookie中username的值
	 * @param arr       $user  获取当前登录用户信息
	 * @param int       $uid   当前登录用户的ID
	 * @param arr       $arts  获取当前登录用户的所有文章
	 * 
	 */
    public function personal(){
    	$name = cookie('username');
    	$user = D('users')->where('username='.'"'.$name.'"')->find();
    	$uid = $user['uid'];
    	$arts = D('arts')->where('uid='.'"'.$uid.'"')->select();
    	$this->assign('arts',$arts);
    	$this->display();
    }

 	/**
	 * hansir注册
	 * 
	 *
	 */
 	/*public function reg(){
 		if (IS_POST) {
 			$pos = I();
 			//var_dump($pos);exit();
 			$user = D('users');
 			$username = $pos['username'];
 			if($user->where('username='.'"'.$username.'"')->find()){
 				echo "用户名已存在";exit();
 			}
 			$user->id = '';
 			$user->username = $pos['username'];
 			$user->pwd = $pos['pwd'];
 			$user->email = $pos['email'];
 			$user->mobile = $pos['mobile'];
 			$user->salt = $this->randCode();
 			$user->add();
 			$this->redirect('/Admin/User/login');
 			//var_dump($user);exit();
 		}else{
 			$this->display();
 		} 		
 	}*/
	/**
	 * Jon 注册
	 * 
	 * 
	 */
    public function reg(){
    		if(!IS_POST){
    			$this->display();
    		}else{
    			$user = D('users');
    			//print_r($user);
    			if(!$user->create()){    				
    				echo $user->getError();
    			}else{
    				$user = D('users');
    				$u = $user ->select();
    				$pos = I();
    				if($u['email']===$pos['email']){
    					echo '该邮箱已使用';
    				    exit();
    				}
    				
    				//$user->add();   				
    				$yan = $this->yan();
    				//把post传递过来的密码的明文密码加盐（乱码）->MD5传给密码
    				
    				$user->pwd = md5($user->pwd.$yan);
    				//把盐加到表中
    				$user->salt = $yan;
    				//凑齐所有字段加到数据库中
    				if($user->add()){
    					$this->redirect('Admin/User/login');
    				}
    			}
 
 
    		}
   		
   }



   /**
    * Jon 盐
    */
   
   public function yan(){
   		return mt_rand(1000,9999);
   }


/**
 * Jon 登陆
 */
public function login(){
	if(!IS_POST){
		$this->display();
	}else{
		//var_dump(I('post.yzm'));
		if($this->checkyzm(I('post.yzm'))){
			// 对用户查询
			//D('users')->where('username='.'"'.I('post.username').'"')原拼
			$username = I('post.username');
			$user = D('users')->where("username='$username'")->find();
			//var_dump($user);
			if(md5(I('post.pwd').$user['salt'])===$user['pwd']){
				cookie('username',$user['username']);
				cookie('coode',md5($user['username'].C('COOKIE_SALT')));
				$this->redirect('/');
			}else{
				echo '用户名或者密码错误';
			}
		}
	}
	
}



/**
 * Jon 验证码
 */

public function yzm(){
	$Verify = new \Think\Verify();
	$Verify->imageW = 120;
	$Verify->imageH = 30;
	$Verify->fontSize = 18;
	$Verify->length = 4;
	$Verify->useNoise = false;
	$Verify->useCurve = false;
	$Verify->entry();
}

/**
 * Jon验证码检测
 */

public function checkyzm($yzm){
	
	$Verify = new \Think\Verify();
	if($Verify->check($yzm)){
		return true;
	}else{
		echo "验证码错误";
		exit();
	}

}







    /**
	 * 登录
	 * if
	 *
	 */
	

   /* public function logIn(){
    	$coo = $this->cookie_check();
    	if ($coo == 1) {
    		$this->redirect('/Admin/User/artlist');
    	}else{
    		if (!IS_POST) {
    			$this->display();
    		}else{
    			$info = I();
    			$uname = $info['username'];
    			$user = D('users');
    			$pwd = $user -> where('username='.'"'.$uname.'"')-> find();
    			if (!isset($pwd['pwd'])) {
    				$pwd['pwd'] = $this->randCode();
    			}
        		if ($info['pwd'] === $pwd['pwd']) {
	    			cookie('username',$pwd['username']);
	            	cookie('coode',md5($pwd['username'].C('COOKIE_SALT')));
    			//var_dump(cookie('username'));exit();
	    			$this->redirect('/Admin/Index/index');
    			}else{
    				echo "用户名或密码错误";
    			}
    		}
    	}
    }*/


    /**
	 * 退出
	 * 
	 *
	 */
    public function logOut(){
    	cookie('username',null);
    	cookie('coode',null);
		$this->redirect('/');
    }

    /**
	 * 检查cookie
	 * 
	 *
	 */

    function cookie_check(){
	    if (cookie('username')) {
		    if(md5(cookie('username').C('COOKIE_SALT')) === cookie('coode')){
		        return 1;
		    }else{
		        return 0;
		    }
	    }else{
	    	return 0;
	    }
    }

    /**
	 * 生成随机字符串
	 * @param int       $length  要生成的随机字符串长度
	 * @param string    $type    随机码类型：0，数字+大小写字母；1，数字；2，小写字母；3，大写字母；4，特殊字符；-1，数字+大小写字母+特殊字符
	 * @return string
	 */
	function randCode($length = 6, $type = 0) {
	    $arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|");
	    if ($type == 0) {
	        array_pop($arr);
	        $string = implode("", $arr);
	    } elseif ($type == "-1") {
	        $string = implode("", $arr);
	    } else {
	        $string = $arr[$type];
	    }
	    $count = strlen($string) - 1;
	    $code = '';
	    for ($i = 0; $i < $length; $i++) {
	        $code .= $string[rand(0, $count)];
	    }
	    return $code;
	}
    //

}